import axios from 'axios';
import router from '@/router';
import  { message } from "ant-design-vue"
let service = axios.create({
  timeout: 30000
});

service.interceptors.request.use(
  config => {
    let accessToken = sessionStorage.getItem('accessToken')
    if (accessToken) {
      config.headers['accessToken'] = accessToken
    } else {
      if (router.history.current.name!='Login') {
        router.push({
          path: '/'
        });
        return Promise.reject();
      }
    }
    return config;
  },
  error => {
    console.log(error);
    return Promise.reject();
  }
);

service.interceptors.response.use(
  response => {
    if (response.data.resultStatus) {
      return response.data;
    } else if (response.data.resultStatus === false) {
      message.error(response.data.errorMessage)
    } else {
      return response.data
    }
    if (response.data.errorCode === '100007') {
      router.push({
        path: '/'
      });
    }
  },
  error => {
    console.log(error);
    return Promise.reject();
  }
);

export default service;